package com.ll.springai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/image")
@RestController
@RequiredArgsConstructor
public class ImageModelController {

    private final OpenAiImageModel openaiImageModel;

    @GetMapping("/getImage")
    public String getImage(@RequestParam(value = "msg", defaultValue = "生成一直小猫") String msg) {
        ImageResponse response = openaiImageModel.call(
            new ImagePrompt(
                msg,
                OpenAiImageOptions.builder()
                    .withQuality("hd")//将生成的图像的质量。HD 创建的图像具有更精细的细节和更高的图像一致性。只有 dall-e-3 支持此参数。
                    .withModel(OpenAiImageApi.DEFAULT_IMAGE_MODEL)
                    .withN(1)//要生成的图像数。必须介于 1 和 10 之间。对于 dall-e-3，仅支持 n=1。
                    .withHeight(1024)//生成的图像的高宽度。必须是 dall-e-2 的 256、512 或 1024 之一。
                    .withWidth(1024).build())
        );
        return response.getResult().getOutput().getUrl();
    }
}

